<template>
  <el-card class="m-b-20">
    <el-table :data="socialData" :show-header="false">
      <el-table-column label="社交平台" align="left" width="120">
        <template slot-scope="scope">
          <img style="height:20px;vertical-align: middle;" :src="scope.row.image" /> {{ scope.row.title }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="right" >
        <template slot-scope="scope">
          <div v-if="scope.row.openid">
            已绑定
            <el-button size="large" type="text" @click="unbind(scope.row)">(解绑)</el-button>
          </div>
          <div v-else>
            未绑定
            <el-button size="large" type="text" @click="bind(scope.row)">(绑定)</el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>
  </el-card>
</template>

<script>
import { getSocialData, getSocialBind } from '@/api/system/user'
export default {
  name: 'UserSocial',
  props: {
    user: {
      type: Object,
      default: () => {
        return {
          userId: undefined
        }
      }
    }
  },
  data() {
    return {
      bindSocial: [],
      socialData: []
    }
  },
  methods: {
    loadData() {
      const that = this
      this.$http.all([getSocialData(), getSocialBind(this.user.userId)])
        .then(this.$http.spread(function(response1, response2) {
          that.socialData = response1.param
          that.bindSocial = response2.param

        for (const i in that.socialData) {
            for (const j in that.bindSocial) {
              if (that.socialData[i].type === that.bindSocial[j].type) {
                that.socialData[i].openid = that.bindSocial[j].openid
                break
              }
            }
          }
      }))
    },
    bind(social) {
      this.$http.get(`${process.env.VUE_APP_MODULE_ADMIN_API}/oauth/social/redirect/` + social.type, {
        params: {
          'redirectUri': location.origin + '/register?type=' + social.type + '&redirect=' + this.$route.path
        }
      }).then(response => {
        window.location.href = response.param
      }).catch(() => {})
    },
    unbind(social) {
      this.$http.get(`${process.env.VUE_APP_MODULE_ADMIN_API}/oauth/social/unbind_oauth/` + social.type, {
        params: {
          'openid': social.openid
        }
      }).then(response => {
        social.openid = undefined
        this.$modal.msgSuccess(response.msg)
      }).catch(() => {})
    }
  }
}
</script>

<style lang="scss" scoped>
.box-center {
  margin: 0 auto;
  display: table;
}

.text-muted {
  color: #777;
}

.user-profile {
  .user-name {
    font-weight: bold;
  }

  .box-center {
    padding-top: 10px;
  }

  .user-role {
    padding-top: 10px;
    font-weight: 400;
    font-size: 14px;
  }

  .box-social {
    padding-top: 30px;

    .el-table {
      border-top: 1px solid #dfe6ec;
    }
  }

  .user-follow {
    padding-top: 20px;
  }
}

.user-bio {
  margin-top: 20px;
  color: #606266;

  span {
    padding-left: 4px;
  }

  .user-bio-section {
    font-size: 14px;
    padding: 15px 0;

    .user-bio-section-header {
      border-bottom: 1px solid #dfe6ec;
      padding-bottom: 10px;
      margin-bottom: 10px;
      font-weight: bold;
    }
  }
}
</style>
